﻿#!/bin/bash
# Filename:248Deploy
# Author:testdream
# Date:20160112

#脚本使用说明
#1、通过指定命令行的第1个参数以确定部署哪个服务，如部署全部则参数为all.
#all 全部更新
#translator 更新248的媒体转换服务
#restapi
#appstore
#session
#organization
#clientcenter
#schd
#manager  管理后台
#serveplatform
#terminal
#poggyio-apn
#mbox
#safeguard
#social
#poggyio-uc
#domainadmin
#meeting
#poggyio-ldap
#poggyio-mdm
#poggyio-pan

#host变量定义
remoteAddr=xxx.xxx.xxx.25
remoteAddr1=xxx.xxx.xxx.23
apiHost=xxx.xxx.xxx.247
managerHost=xxx.xxx.xxx.246
ImHost=xxx.xxx.xxx.250
localhost=xxx.xxx.xxx.248

#路径定义
deployfiles=/xxx/test/autoDeploy
remotePath=/xxx/poggyio
remoteManPath=/xxx/poggyio/apache-tomcat-8.0.24/webapps/poggyio
targetPath=/xxx/test/workplus

#复制文件夹到本地172.16.1.248
if [ -z $2 ];then		
	echo "================================Delete files now!=============================="
	rm -rf $deployfiles/files/*
	echo "================================Copy File Now!================================"
	scp -r -P 22 test@$remoteAddr:$remotePath/poggyio-*  $deployfiles/files/

    scp -r -P 22 root@$remoteAddr1:$remotePath/poggyio-*  $deployfiles/files/

	mkdir $deployfiles/files/manager
	scp -r -P 22 test@$remoteAddr:$remoteManPath/*  $deployfiles/files/manager/

	#删除其它文件
	for sinFile in $deployfiles/files/*;do
		if [ -f $sinFile ];then
			rm -f $sinFile
		fi
	done
fi

#执行函数-参数1为部署目标
function deploy248(){
	#echo "===============================BackFiles========================================"
	#rm -rf $targetPath/bakFiles/*$1*
	echo "=================================Deploy now!===================================="
	i=0
	#覆盖配置文件
	echo "==============================Update config file!==============================="
	cd $deployfiles/files
	for sinFile in `find ./ -type d  -name  "*$1*"`;do
	echo $sinFile
		if [ -d $sinFile ];then
			cp -rf $deployfiles/conf/$1/* $sinFile
			i=$[ $i+1 ]
		else
			rm -f $sinFile
		fi
	done
	#文件夹是否多于1个
	if [ $i -gt 1 ];then
		echo "=============Oops! The $1 folder more then one!Stop!===================="
		exit 1
	fi
		
		
	#备份删除服务
	echo "==========================Stop service and backup files!=========================="
	#pid_$1=`netstat -ntpl|grep 8083|awk -F[/] '{print $1}'|awk '{print $7}'`
	pid=`jps -v |grep $1|awk '{print $1}'`
	if [ -n "$pid" ];then
		kill -9 $pid
		echo "============The PID of $1 is deleted successfully!====================="
	fi
		
	#删除软链
	cd 	$targetPath
	rm $1
	#备份并更新名称
	i=0
	for sinFold in `find ./ -type d  -name  "*$1*"`;do
	echo $sinFold
		if [ -d $sinFold ];then
			mv $targetPath/*$1*  $targetPath/bakFiles/$(basename $sinFold)-$(date +%y%m%d%H%M%S)		
			i=$[ $i+1 ]
		else
			rm -f $sinFold
		fi
	done
	#文件夹是否多于1个
	if [ $i -gt 1 ];then
		echo "=============Oops! The $1 folder more then one!Stop!===================="
	fi
	
	rm -rf $targetPath/*$1*
		
	#复制目标文件夹
	cp -r $deployfiles/files/*$1*  $targetPath/
		
	#启动应用
	echo "==========================Add soft link and start service!========================="
	ln -s $targetPath/*$1*  $targetPath/$1
	
	if [ -n "$pid" ];then
		j=0
		while(($j<60))
		do
			#echo $pid
			port=`netstat -ntpl|grep $pid |awk '{print $1}'`
			if [ -z "$port" ];then
				break
			fi
			sleep 2
			j=$[ $j+1 ]
		done
	fi
	
	sh  $targetPath/$1/bin/start.sh
}


#248服务器部署
echo "=================================Deploy now!===================================="
case $1 in 
	translator|all)
		pid_office=`ps -ef|grep soffice|grep -v grep|awk '{print $2}'`
		if [ -n "$pid_office" ];then
			kill -9 $pid_office
			echo "================The PID of office is deleted successfully!====================="
		fi
		
		deploy248 translator
	;;&
	safeguard|all)
		deploy248 safeguard
	;;&
	poggyio-ldap|all)
		deploy248 poggyio-ldap
	;;&
	
	appstore|all)
		echo "==========================Deploy the appstore of 247!========================="
		ssh -p22 $apiHost "sh $deployfiles/247Deploy.sh appstore"
	;;&
	restapi|all)
		echo "==========================Deploy the restapi of 247!========================="
		ssh -p22 $apiHost "sh $deployfiles/247Deploy.sh restapi"
	;;&
	poggyio-uc|all)
		echo "==========================Deploy the poggyio-uc of 247!========================="
		ssh -p22 $apiHost "sh $deployfiles/247Deploy.sh poggyio-uc"
	;;&
	session|all)
		#echo "==========================Deploy the session of 247!========================="
		#ssh -p22 $apiHost "sh $deployfiles/247Deploy.sh session"
		#echo "==========================Deploy the session of 250!========================="
		#ssh  $ImHost "sh $deployfiles/250Deploy.sh session"	
                echo "==========================Deploy the session of 248!========================="
                deploy248 session
	;;&
	organization|all)
		echo "==========================Deploy the organization of 247!========================="
		ssh -p22 $apiHost "sh $deployfiles/247Deploy.sh organization"
	;;&
	clientcenter|all)
		echo "==========================Deploy the clientcenter of 247!========================="
		ssh -p22 $apiHost "sh $deployfiles/247Deploy.sh clientcenter"
	;;&
	social|all)
		echo "==========================Deploy the social of 247!========================="
		ssh -p22 $apiHost "sh $deployfiles/247Deploy.sh social"
	;;&
	
	schd|all)
		echo "==========================Deploy the schd of 250!========================="
		ssh  $ImHost "sh $deployfiles/250Deploy.sh schd"
	;;&
	mbox|all)
		echo "==========================Deploy the mbox of 250!========================="
		ssh  $ImHost "sh $deployfiles/250Deploy.sh mbox"
	;;&

	poggyio-mdm|all)
		echo "==========================Deploy the poggyio-mdm of 250!========================="
		ssh  $ImHost "sh $deployfiles/250Deploy.sh poggyio-mdm"
	;;&

	poggyio-pan|all)
		echo "==========================Deploy the poggyio-pan of 250!========================="
		ssh  $ImHost "sh $deployfiles/250Deploy.sh poggyio-pan"
	;;&
	
	manager|all)
		echo "==========================Deploy the manager of 246!========================="
		ssh  -p22 $managerHost "sh $deployfiles/246Deploy.sh manager"
	;;&
	poggyio-apn|all)
		echo "==========================Deploy the poggyio-apn of 246!========================="
		ssh  -p22 $managerHost "sh $deployfiles/246Deploy.sh poggyio-apn"
	;;&
	domainadmin|all)
		echo "==========================Deploy the domainadmin of 246!========================="
		ssh -p22 $managerHost "sh $deployfiles/246Deploy.sh domainadmin"
	;;&
	serveplatform|all)
		echo "==========================Deploy the serveplatform of 246!========================="
		ssh  -p22 $managerHost "sh $deployfiles/246Deploy.sh serveplatform"
	;;&
	terminal|all)
		echo "==========================Deploy the terminal of 247!========================="
		ssh -p22 $apiHost "sh $deployfiles/247Deploy.sh terminal"
	;;&
	meeting|all)
		echo "==========================Deploy the meeting of 250!========================="
		ssh $ImHost "sh $deployfiles/250Deploy.sh meeting"
	;;&

esac

exit 0 
